package com.snake.scoreboardlib;

import java.util.Date;
import java.util.Iterator;

import org.json.JSONException;
import org.json.JSONObject;


public class ScoreboardItem {
	private static final String jfnDUID = "duid";
	private static final String jfnUsername = "username";
	private static final String jfnScores = "scores";
	private static final String jfnDate = "date";
	
	
	

	//id of device
	private String duid;
	//username
	private String username;
	//scores
	private int scores;
	private Date date;
	
	
    public ScoreboardItem(String username, int scores)
    {
    	this();
    	this.username = username;
    	this.scores = scores;
    }
	public ScoreboardItem()
	{
		date = new Date();
	}
	public String getDUID()
	{
		return duid;
	}
	
	public void setDUID(String duid)
	{
		this.duid = duid;
	}
	
	public String getUsername()
	{
		return username;
	}
	
	public void setUsername(String username)
	{
		this.username = username;
	}
	
	public int getScores()
	{
		return scores;
	}
	public void setScores(int scores)
	{
		this.scores = scores;
		
	}
	public JSONObject getJSONObject() throws JSONException
	{
		JSONObject jo = new JSONObject();
		
		jo.put(jfnDUID, duid);
		jo.put(jfnUsername, username);
		jo.put(jfnScores, 	Integer.toString(scores));
		jo.put(jfnDate, 	OSHelper.DateToStr(date));
		return jo;
	}
	public String toJSON() throws JSONException
	{
		
			return getJSONObject().toString();
	}
	public void fromJSONObject(JSONObject jo) throws JSONException
	{
		Iterator iter = jo.keys();
		//parse json obj
		while (iter.hasNext())
		{
			Object obj = iter.next();
			String key = obj.toString();
			String value = jo.getString(key);
			if (key.compareToIgnoreCase(jfnDUID) == 0)
				duid = value;
		
			if (key.compareToIgnoreCase(jfnUsername) == 0 )
				username = value;

			if (key.compareToIgnoreCase(jfnScores) == 0)
				scores = Integer.parseInt(value);

			if (key.compareToIgnoreCase(jfnDate) == 0 )
				this.date = OSHelper.StrToDateDef(value, new Date());
								
							
						
			
		}

	}
	public void fromJSON(String json) throws JSONException
	{
		
			JSONObject jo = new JSONObject(json);
			fromJSONObject(jo);		
		
		
		
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	
	

}
